#include "DHT.h"
#include "sensor.h"

Sensor::Sensor(uint8_t pin) : 
_dht(pin, DHT11) {
  _failedAttempts = 0;
} 

void Sensor::init() {
  _dht.begin();
  // read it and delay to make sure another sensor is not read at the same time.
  _dht.readTemperature();
  delay(2000);
}

int Sensor::getTemperature() {
  int t = _dht.readTemperature();
  if(isnan(t)) {
    _failedAttempts++;
  } 
  else {
    _failedAttempts = 0;
    _temperature = t;
  }
  return _temperature;
}

int Sensor::getHumidity() {
  return _dht.readHumidity();
}

int Sensor::getFailedAttempts() {
  return _failedAttempts;
}

String Sensor::getStatus() {
  // call getters to re-read data from sensors
  return String("T=") + getTemperature() 
    + ",H=" + getHumidity() 
    + ",E=" + _failedAttempts;
}

